package controller

import (
	"parking-lot-go-admin/config"

	"github.com/gin-gonic/gin"
)

func Upload(c *gin.Context) {
	file, err := c.FormFile("file")
	if err != nil {
		Fail(c, "上传文件失败")
	}

	// 生成随机文件名
	randomFileName := GenerateRandomFileName(file.Filename)

	// 将文件保存到指定路径
	err = c.SaveUploadedFile(file, "./uploads/"+randomFileName)
	if err != nil {
		Fail(c, "保存文件失败")
	}

	systemSection := config.Cfg.Section("SYSTEM")
	imgUrl := systemSection.Key("IMAGE_URL").String()
	Success(c, map[string]interface{}{
		"url": imgUrl + randomFileName,
	})
}
